package com.smdk.dsminio.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smdk.dsminio.dao.DsminioFileInfoMapper;
import com.smdk.dsminio.entity.DsminioBucketInfo;
import com.smdk.dsminio.entity.DsminioFileInfo;
import com.smdk.dsminio.entity.DsminioFolderInfo;
import com.smdk.dsminio.entity.DsminioServerNode;
import com.smdk.dsminio.service.DsminioBucketInfoService;
import com.smdk.dsminio.service.DsminioFileInfoService;
import com.smdk.dsminio.service.DsminioFolderInfoService;
import com.smdk.dsminio.service.DsminioServerNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (DsminioObjectInfo)表服务实现类
 *
 * @author 神秘的凯
 * @since 2020-10-28 15:12:36
 */
@Service
public class DsminioObjectInfoServiceImpl extends ServiceImpl<DsminioFileInfoMapper, DsminioFileInfo> implements DsminioFileInfoService {
    @Autowired
    private DsminioFolderInfoService dsminioFolderInfoService;
    @Autowired
    private DsminioServerNodeService dsminioServerNodeService;
    @Autowired
    private DsminioBucketInfoService dsminioBucketInfoService;
    @Override
    public boolean addFileInfo(DsminioFileInfo dsminioFileInfo) {
        boolean result=save(dsminioFileInfo);
        //FolderSize更新
        DsminioFolderInfo dsminioFolderInfo = dsminioFolderInfoService.getById(dsminioFileInfo.getFolderId());
        dsminioFolderInfo.setFileCount(dsminioFolderInfo.getFileCount()+1);
        dsminioFolderInfo.setFolderSize(dsminioFolderInfo.getFolderSize()+dsminioFileInfo.getSize());
        dsminioFolderInfoService.updateDsminioFolderInfo(dsminioFolderInfo);
        //bucketSize更新
        DsminioBucketInfo dsminioBucketInfo =dsminioBucketInfoService.getById(dsminioFolderInfo.getBucketId());
        dsminioBucketInfo.setBucketSize(dsminioBucketInfo.getBucketSize()+dsminioFileInfo.getSize());
        dsminioBucketInfo.setBucketObjectCount(dsminioBucketInfo.getBucketObjectCount()+1);
        //Node节点空间更新
        DsminioServerNode dsminioServerNode = dsminioServerNodeService.getById(dsminioFileInfo.getNodeServerId());
        dsminioServerNode.setNodeDiskSize(dsminioServerNode.getNodeDiskSize()+dsminioFileInfo.getSize());
        dsminioServerNodeService.updateById(dsminioServerNode);
        return result;
    }

    @Override
    public boolean deleteFileInfo(List<Long> fileIds) {
        return removeByIds(fileIds);
    }

    @Override
    public boolean updateFileInfo(DsminioFileInfo dsminioFileInfo) {
        return this.updateById(dsminioFileInfo);
    }

    @Override
    public List<DsminioFileInfo> searchFileInfoByFolderId(Long folderId) {
        return this.query().eq("folderId",folderId).list();
    }

    @Override
    public List<DsminioFileInfo> searchOFileInfoByBucketId(Long bucketId) {
        return this.query().eq("bucketId",bucketId).list();
    }
}